
import sys
from distutils.util import get_platform

plat_specifier = ".%s-%s" % (get_platform(), sys.version[0:3])
sys.path.append("./build/lib%s" % (plat_specifier))

import jjregion.parser as RP


test_string_1 = r""" # cooo
circle(109,253,28.950304) # comment
polygon(257,290,300.78944,271.78944,300.78944,178.21056,258,216,207.21056,178.21056)
polygon(273.98971,175.01029,274.01029,175.01029,274.01029,174.98971,273.98971,174.98971)
-rotbox(162,96.5,134,41,43.801537)
and ellipse(172,328,23,41,27.300813)"""
test_string_1_shape_name_list = ["circle", "polygon", "polygon", "rotbox", "ellipse"]

#test_list = r"""circle(109,253,28.950304)"""

def test_factory_skeleton():

    rf = RP.RegionFactorySkeleton()
    rp = RP.RegionParser(rf)

    a= rp.parseString(test_string_1)
    assert a == "((((circle | polygon) | polygon) - rotbox) & ellipse)"





def test_factory_tuple_list():
    rf = RP.RegionFactoryTupleList()
    rp = RP.RegionParser(rf)

    a= rp.parseString(test_string_1)
    b = [region_name for region_name, region_coord in a]
    
    print b
    assert b == test_string_1_shape_name_list


def test_sexagesimal():
    rf = RP.RegionFactoryTupleList()
    rp = RP.RegionParser(rf)

    test_string = "circle(109:23:2.2,-253:32.2,28.950304')"

    a= rp.parseString(test_string)

    s = a[0]
    assert s[1][0] == "109:23:2.2"
    assert s[1][1] == "-253:32.2"
    assert s[1][2] ==  "28.950304'"

    test_string = "circle(-109:23:2.2,253:32:23.2,28'32\")"
    ss = "-109:23:2.2,253:32:23.2,28'32\"".split(",")
    
    a= rp.parseString(test_string)

    s = a[0]
    assert s[1][0] == ss[0]
    assert s[1][1] == ss[1]
    assert s[1][2] == ss[2]


class test_region_factory_simple(object):
    def __init__(self):
        self.rf = RP.RegionFactorySimple()
        self.rp = RP.RegionParser(self.rf)

    def test_unknown_shape(self):
        input_string = "triangle(109,253,28.950304)"
        try:
            r = self.rp.parseString(input_string)
        except self.rf.UnknownShape:
            pass
        else:
            raise "Unknown shape exception is expected but not raise"
        

    def test_circle(self, ):
        """
        """
        input_string = "circle(109,253,28.950304)"
        r = self.rp.parseString(input_string)
        assert r[0][0] == "circle"

    def test_a1(self, ):
        input_string = test_string_1
        r = self.rp.parseString(input_string)
        assert [shape_name for shape_name, shape_numbers in r] == test_string_1_shape_name_list

